package com.example.demo.mvvm.ui.fragment;

import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;

import com.example.demo.mvvm.R;
import com.example.demo.mvvm.anim.CircularRevealAnim;
import com.example.demo.mvvm.databinding.DialogFragmentSearchBinding;
import com.example.demo.mvvm.ui.vm.fragment.SearchFragmentVm;

/**
 * Created by 80711 on 2018/5/21.
 */

public class SearchFragment extends DialogFragment implements CircularRevealAnim.AnimListener,
        ViewTreeObserver.OnPreDrawListener, DialogInterface.OnKeyListener,
        View.OnClickListener{
    DialogFragmentSearchBinding binding;
    CircularRevealAnim mCircularRevealAnim;
    SearchFragmentVm searchFragmentVm;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_fragment_search, container, false);
        searchFragmentVm = new SearchFragmentVm(binding.etSearchKeyword);
        binding.setViewModel(searchFragmentVm);
        searchFragmentVm.getHotKeyDatas(binding.pullRefresh);
        binding.executePendingBindings();
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        mCircularRevealAnim = new CircularRevealAnim();
        mCircularRevealAnim.setAnimListener(this);
        getDialog().setOnKeyListener(this);
        binding.ivSearchSearch.setOnClickListener(this);
        binding.ivSearchBack.setOnClickListener(this);
        binding.ivSearchSearch.getViewTreeObserver().addOnPreDrawListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        DisplayMetrics metrics = getResources().getDisplayMetrics();

        //int width = (int) (metrics.widthPixels * 0.98);

        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT , WindowManager.LayoutParams.MATCH_PARENT);

        window.setGravity(Gravity.TOP);

        window.setWindowAnimations(R.style.DialogEmptyAnimation);

    }

    @Override
    public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
        if (i == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
            hideAnim();
        } else if (i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
            search();
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_search_search:
                search();
                break;
            case R.id.iv_search_back:
                hideAnim();
                break;
        }
    }

    @Override
    public boolean onPreDraw() {
        binding.ivSearchSearch.getViewTreeObserver().removeOnPreDrawListener(this);
        mCircularRevealAnim.show(binding.ivSearchSearch, binding.getRoot());
        return true;
    }

    @Override
    public void onHideAnimationEnd() {
        binding.etSearchKeyword.setText("");
        dismiss();
    }

    @Override
    public void onShowAnimationEnd() {
        if (this.isVisible()) {
            //KeyBoardUtils.openKeyboard(activity, et_search_keyword);
        }
    }

    private void search() {
        searchFragmentVm.searchDatas(binding.pullRefresh);
    }

    private void hideAnim() {
        //KeyBoardUtils.closeKeyboard(activity, et_search_keyword);
        mCircularRevealAnim.hide(binding.ivSearchSearch, binding.getRoot());
    }
}


